﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using POSSystem.BLL;

namespace WebUI.Controls
{
    public partial class InvoiceViewerCtrl : BaseEditControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                lblCompany.Text = Branches.CurrentBranch.s_Name;
                lblAddress.Text = Branches.CurrentBranch.s_Address;
                lblABN.Text = Branches.CurrentBranch.s_ABN;
                lblFax.Text = Branches.CurrentBranch.s_Fax;
                lblPhone.Text = Branches.CurrentBranch.s_Phone;
                litFooter.Text = SysVars.InvoiceFooter;
                
            }
        }

        protected void btnLoad_OnClick(object sender, EventArgs e)
        {
            if (hidID.Value != "")
                LoadInvoiceByID(int.Parse(hidID.Value));
        }

        public void LoadInvoiceByNo(string no)
        {
            View_Invoices ivc = View_Invoices.GetInvoiceByNo(no);
            LoadInvoice(ivc);
        }

        public void LoadInvoiceByID(int invoiceID)
        {
            View_Invoices ivc = View_Invoices.GetInvoiceByID(invoiceID);
            LoadInvoice(ivc);
        }

        private void LoadInvoice(View_Invoices ivc)
        {
            if (ivc.RowCount <= 0)
                return;

            litNote.Text = "<b>Note:</b>" + ivc.s_Note;
            if (ivc.s_CreatedDate != "")
                lblDate.Text = ivc.CreatedDate.ToString(SysVars.DateFormat);

            lblSalePerson.Text = ivc.s_SaleUserFullName;

            if (ivc.PaidAmount >= ivc.TotalAmount)
                lblPaymentStatus.Text = "FULL PAID";
            else
                lblPaymentStatus.Text = String.Format("<a class='customhyperlink1' {1} >{0:C} UNPAID </a>", ivc.TotalAmount - ivc.PaidAmount, SessionUser.BranchID == Branches.CurrentBranchID ? string.Format(" onclick='pay({0});'", ivc.ID) : "");

            View_InvoicePaymentHist payHist = View_InvoicePaymentHist.GetPaymentHistories(ivc.ID);
            for (int i = 0; i < payHist.RowCount; i++)
            {
                litPaymentHist.Text += payHist.GetPaymentString() + "<br/>";
                payHist.MoveNext();
            }

            lblInvoiceNo.Text = ivc.InvoiceNo;
            divInvoiceNo.InnerHtml = Utilities.GenerateBarCode(ivc.InvoiceNo);

            ivcDetails.ShowFooter = true;
            ivcDetails.SourceObject = View_InvoiceDetails.GetDetailsByInvoiceID(ivc.ID);
            ivcDetails.UpdateTotalAmt();
            ivcDetails.Refresh();

            lblTotalAmt.Text = string.Format("{0:C}", ivc.TotalAmount);
            //lblTotalGST.Text = string.Format("{0:C}", ivc.GSTTotalAmount);
            if (ivc.s_Discount != "")
            {
                if(ivc.s_SpecialDiscount != "")
                    lblSpecialDiscount.Text = string.Format("{0:C}", ivc.SpecialDiscount);
                else if(ivc.s_DiscountAmount != "")
                    lblSpecialDiscount.Text = string.Format("{0:C}", ivc.DiscountAmount);
                else
                    lblSpecialDiscount.Text = string.Format("{0}%", ivc.DiscountPercent);
                lblSpecialDiscount.Visible = true;
                lblSpecialDiscountLabel.Visible = true;
            }
            lblCustomer.Text = ivc.IsColumnNull(View_Invoices.ColumnNames.CustomerID) ? ivc.s_CustomerName : ivc.s_CustName;
            if (ivc.s_Address != "")
                lblCustomer.Text += " - " + ivc.s_Address;
        }
    }
}